﻿#include "customlistitem.h"



CustomListItem::CustomListItem()
{

}

CustomListItem::CustomListItem(QWidget *parent):QWidget(parent)
{
    this->style = ItemStyle::Nomarl;
    this->name = "名称";
    this->init();
}

CustomListItem::CustomListItem(CustomListItem::ItemStyle style, QString name, QString category,bool isFinish)
{
    this->name = this->key = name;
    this->style = style;
    this->isFinish = isFinish;
    this->isSelect =false;
    this->category = category;
    this->init();
    this->bindEvent();
}

CustomListItem::CustomListItem(ItemModel &item, CustomListItem::ItemStyle style)
{
    // 加载模型数据
    this->name = item.name;
    this->key = item.id;
    this->isFinish = true;
    this->isSelect =false;
    this->style = style;
    this->init();
    this->bindEvent();
}


void CustomListItem::init()
{
    // 初始化控件
    QHBoxLayout* layout = new QHBoxLayout();
    layout->setSpacing(0);
    layout->setMargin(0);


    this->setMinimumHeight(80);
    this->setMaximumHeight(80);

    this->nameL = new QLabel();
    this->iconL = new QLabel();
    this->leftIconL = new QLabel();
    this->rightIconL = new QLabel();

    // 文本框
    QSizePolicy policy;
    policy.setHorizontalPolicy(QSizePolicy::Fixed);
    policy.setVerticalPolicy(QSizePolicy::Preferred);
    nameL->setText(name);
    nameL->setStyleSheet("background: transparent;font-size:36px;color:white;text-indent:0px;border:none;");
    nameL->setSizePolicy(policy);
    nameL->setMargin(0);
//    nameL->setMinimumSize(QSize(80,10));


    switch (this->style) {
        case ItemStyle::Nomarl: {
            layout->setContentsMargins(0, 0, 20, 0);

            QHBoxLayout* centerBox = new QHBoxLayout();
            centerBox->setContentsMargins(85, 0, 0, 0);

            QSpacerItem* leftS = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Fixed);
            QSpacerItem* rightS = new QSpacerItem(40,20, QSizePolicy::Expanding, QSizePolicy::Fixed);

            // 内容盒子中放置内容
            centerBox->addSpacerItem(leftS);
            centerBox->addWidget(nameL);
             // 初始化右侧的选中图标
            this->initIconL();
            centerBox->addWidget(iconL);
            centerBox->addSpacerItem(rightS);
            centerBox->setSpacing(15);

            QSizePolicy policy3;
            policy3.setHorizontalPolicy(QSizePolicy::Fixed);
            policy3.setVerticalPolicy(QSizePolicy::Fixed);
            rightIconL->setSizePolicy(policy3);
            rightIconL->setMinimumSize(QSize(40,10));
            rightIconL->setMaximumSize(QSize(19,19));
            rightIconL->setStyleSheet("background: transparent;border:none;");
            layout->addItem(centerBox);
            layout->addWidget(rightIconL);
            break;
        }
        case ItemStyle::PairIcon:{
            layout->setContentsMargins(48, 0, 48, 0);
            QHBoxLayout* centerBox = new QHBoxLayout();
            centerBox->setContentsMargins(45, 0, 0, 0);
            QSpacerItem* leftS = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Fixed);
            QSpacerItem* rightS = new QSpacerItem(40,20, QSizePolicy::Expanding, QSizePolicy::Fixed);

            // 内容盒子中放置内容
            centerBox->addSpacerItem(leftS);
            centerBox->addWidget(nameL);
            this->initIconL();
            centerBox->addWidget(iconL);
            centerBox->addSpacerItem(rightS);
            centerBox->setSpacing(15);

            // 左右两边的图标
            QSizePolicy policy3;
            policy3.setHorizontalPolicy(QSizePolicy::Fixed);
            policy3.setVerticalPolicy(QSizePolicy::Fixed);
            leftIconL->setSizePolicy(policy3);
            leftIconL->setMinimumSize(QSize(9,11));
            leftIconL->setMaximumSize(QSize(9,11));
            leftIconL->setStyleSheet("background: transparent;border:none;");
            rightIconL->setSizePolicy(policy3);
            rightIconL->setMinimumSize(QSize(9,11));
            rightIconL->setMaximumSize(QSize(9,11));
            rightIconL->setStyleSheet("background: transparent;border:none;");
            layout->addWidget(leftIconL);
            layout->addItem(centerBox);
            layout->addWidget(rightIconL);
            break;
        }
        case ItemStyle::Column:{

            layout->setContentsMargins(15, 0, 15, 0);
            layout->setSpacing(0);
//            QHBoxLayout* centerBox = new QHBoxLayout();
//            centerBox->setContentsMargins(15, 0, 15, 0);
            QSpacerItem* leftS = new QSpacerItem(125, 20, QSizePolicy::Fixed, QSizePolicy::Preferred);
            QSpacerItem* rightS = new QSpacerItem(125, 20, QSizePolicy::Fixed, QSizePolicy::Preferred);

            // [!0]左右两边的图标
            QSizePolicy policy3;
            policy3.setHorizontalPolicy(QSizePolicy::Fixed);
            policy3.setVerticalPolicy(QSizePolicy::Fixed);
            leftIconL->setSizePolicy(policy3);
            leftIconL->setMinimumSize(QSize(9,11));
            leftIconL->setMaximumSize(QSize(9,11));
            leftIconL->setStyleSheet("background: transparent;border:none;");
            rightIconL->setSizePolicy(policy3);
            rightIconL->setMinimumSize(QSize(9,11));
            rightIconL->setMaximumSize(QSize(9,11));
            rightIconL->setStyleSheet("background: transparent;border:none;");
            // [!0]

            // [!1]左右两边的文本
            QSizePolicy p1;
            p1.setHorizontalPolicy(QSizePolicy::Expanding);
            p1.setVerticalPolicy(QSizePolicy::Preferred);
            nameL->setSizePolicy(p1);
            nameL->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
            nameL->setStyleSheet("background:transparent;font-size:36px;color:white;text-indent:0px;border:none;");

            categoryL = new QLabel();
            categoryL->setText(category);
            categoryL->setStyleSheet("background:transparent;font-size:30px;color:#BBBBBB;text-indent:0px;border:none;");
            QSizePolicy p2;
            p2.setHorizontalPolicy(QSizePolicy::Fixed);
            p2.setVerticalPolicy(QSizePolicy::Preferred);
            categoryL->setSizePolicy(p2);
            categoryL->setMargin(0);
            categoryL->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
            // [!1]

            layout->addWidget(leftIconL);
            layout->addSpacerItem(leftS);
            layout->addWidget(nameL);
            layout->addWidget(categoryL);
            layout->addSpacerItem(rightS);
            layout->addWidget(rightIconL);
            break;
        }
    }

    this->setLayout(layout);
//    this->unSelect();
}

void CustomListItem::initIconL()
{
    // 右边ICON 设置
    QSizePolicy policy2;
    policy2.setHorizontalPolicy(QSizePolicy::Fixed);
    policy2.setVerticalPolicy(QSizePolicy::Fixed);
    iconL->setSizePolicy(policy2);
    iconL->setMinimumSize(QSize(20, 20));
    iconL->setMaximumSize(QSize(20, 20));

    // 设置背景图标
    if (isFinish) {
        iconL->setStyleSheet("background: transparent;border-image: url(:/res/actions-finish-icon.png);");
    } else {
        iconL->setStyleSheet("background: transparent;border-image: url(:/res/actions-unfinish-icon.png);");
    }

}

void CustomListItem::bindEvent()
{
    // 绑定点击事件
    connect(this,  &CustomListItem::clicked, [=](){
        // 设置自己父元素中其他的节点为未选中状态
        QWidget* parent = (QGroupBox*)this->parent();
        QList<CustomListItem*> _items = parent->findChildren<CustomListItem *>();
        CustomListItem *item=NULL;
        for(auto it=_items.begin();it!=_items.end();it++)
        {
            item=*it;
            item->unSelect();
        }

        // 设置自己为选中状态
        this->selected();

    });
}

void CustomListItem::selected()
{

    switch (this->style) {
        case ItemStyle::Nomarl:
            rightIconL->setStyleSheet("background: transparent;border-image: url(:/res/actions-selected-icon.png);");
            break;
        case ItemStyle::PairIcon:
            leftIconL->setStyleSheet("background: transparent;border-image: url(:/res/actions-skill-right-icon.png);");
            rightIconL->setStyleSheet("background: transparent;border-image: url(:/res/actions-skill-left-icon.png);");
            break;
        case ItemStyle::Column:
            leftIconL->setStyleSheet("background: transparent;border-image: url(:/res/actions-skill-right-icon.png);");
            rightIconL->setStyleSheet("background: transparent;border-image: url(:/res/actions-skill-left-icon.png);");
            break;
    }
    // QWidget 的子类增加 OBJECT 宏后，会影响到背景的设置
    //    this->setStyleSheet("QWidget{background:rgba(113,187,63,50);}");
    setAutoFillBackground(true);
    QPalette palette = this->palette();
    palette.setColor(QPalette::Background, QColor(0x71,0xBB,0x3F, 100));
    setPalette(palette);


    // 确保自己在滚动区域中处于可见
    // 1.确认自己在滚动容器中
    // 2.转换类型
    // 3.设置控件处于显示区域
    if (this->parent() != 0 && this->parent()->parent() != 0 && this->parent()->parent()->parent() != 0) {
         QObject* obj = (QObject*)this->parent()->parent()->parent();
         qDebug() << obj->metaObject()->className() << endl;
         if (strcmp(obj->metaObject()->className(), "QScrollArea") == 0) {
             qDebug() << "处于容器中！" << endl;
             QScrollArea* scroll = (QScrollArea*)obj;
             scroll->ensureWidgetVisible(this, 0, 0);
         }
    }

    this->isSelect = true;
}

void CustomListItem::unSelect()
{
    this->setStyleSheet("background:transparent;");
    switch (this->style) {
        case ItemStyle::Nomarl:
            rightIconL->setStyleSheet("background: transparent;border:none;");
            break;
        case ItemStyle::PairIcon:
            leftIconL->setStyleSheet("background: transparent;border:none;");
            rightIconL->setStyleSheet("background: transparent;border:none;");
            break;
        case ItemStyle::Column:
            leftIconL->setStyleSheet("background: transparent;border:none;");
            rightIconL->setStyleSheet("background: transparent;border:none;");
            break;
    }
    this->isSelect = false;
}

bool CustomListItem::isSelected()
{
    return this->isSelect;
}

QString CustomListItem::getKey()
{
    return this->key;
}

void CustomListItem::mousePressEvent(QMouseEvent *event)
{
    //如果单击了就触发clicked信号
    if (event->button() == Qt::LeftButton)
    {
        //触发clicked信号
        emit this->clicked();
    }

}



